﻿using System;
using System.Reflection;
using System.Windows.Forms;

using xfsale.lib;
using xfsale.packet;
using xfsale.domain;
using DevExpress.XtraTab;

namespace xfsale.ui
{
    public partial class FrmBase : UserControl
    {
        private static string TAG_SEPERATOR = "|";
        private string windowTitle;

        public FrmBase()
        {
            InitializeComponent();
        }

        protected void InitCtrl(Control ctrl, string dictName)
        {
            SysControl.InitCtrl(ctrl, dictName);
        }

        protected void ResetCtrl(Control ctrl, string dictName)
        {
            SysControl.ResetCtrl(ctrl, dictName);
        }

        protected string GetCtrlValue(Control ctrl)
        {
            return SysControl.GetCtrlValue(ctrl);
        }

        protected void SetCtrlValue(Control ctrl, string value)
        {
            SysControl.SetCtrlValue(ctrl, value);
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                SysMenuItem menu = Tag as SysMenuItem;

                Width = Parent.Width;
                windowTitle = ParentForm.Text;
                //ParentForm.Text = windowTitle + "  -  " + menu.serviceCode + " " + menu.menuCaption;

                Init();
            }
        }

        protected void CloseFrame()
        {
            //(ParentForm.Controls["Splitter"] as DevExpress.XtraEditors.SplitContainerControl).Panel1.Enabled = true;
            //(ParentForm.Controls["NavPanel"] as DevExpress.XtraEditors.PanelControl).Enabled = true;
            ParentForm.Text = windowTitle;            
            (Parent as XtraTabPage).TabControl.TabPages.Remove(Parent as XtraTabPage);            
            //TabControl TabMain = (Parent as TabPage).Parent as TabControl;
            //TabMain.Controls.Remove(Parent as TabPage);
            this.Dispose();
        }

        protected object CallService(object request)
        {
            SysMenuItem menu = Tag as SysMenuItem;
            if (string.IsNullOrEmpty(menu.callService) || string.IsNullOrEmpty(menu.callMethod))
            {
                SysMessage.Error("调用服务和方法不能为空");
                return null;
            }

            return App.CallService(menu.callService, menu.callMethod, request);
        }

        protected void Bind(object request)
        {
            SysMenuItem menu = Tag as SysMenuItem;
            FieldInfo fi = request.GetType().GetField("serviceCode");
            if (fi != null && fi.IsPublic)
            {
                fi.SetValue(request, menu.serviceCode);
            }
            fi = request.GetType().GetField("regionCode");
            if (fi != null && fi.IsPublic)
            {
                fi.SetValue(request, App.Global.RegionCode);
            }
            fi = request.GetType().GetField("branchCode");
            if (fi != null && fi.IsPublic)
            {
                fi.SetValue(request, App.Global.BranchCode);
            }
            fi = request.GetType().GetField("netCode");
            if (fi != null && fi.IsPublic)
            {
                fi.SetValue(request, App.Global.NetCode);
            }
            fi = request.GetType().GetField("opCode");
            if (fi != null && fi.IsPublic)
            {
                fi.SetValue(request, App.Global.OpCode);
            }
            fi = request.GetType().GetField("transAccountID");
            if (fi != null && fi.IsPublic)
            {
                fi.SetValue(request, App.Customer.TransAccountID);
            }

            BindCtrl(request, this);

            BindDict(request);
        }

        private void BindDict(object request)
        {
            FieldInfo[] fields = request.GetType().GetFields();
            foreach (FieldInfo field in fields)
            {                
                if (field.FieldType == typeof(string))
                {
                    string fieldName = field.Name.ToUpper();
                    string fieldValue = Convert.ToString(field.GetValue(request));
                    if (string.IsNullOrEmpty(fieldValue))
                    {
                        if (App.DictItems.ContainsKey(fieldName))
                        {
                            SysDictItem dictItem = App.DictItems[fieldName] as SysDictItem;
                            field.SetValue(request, dictItem.itemDefault);
                        }
                    }
                }
            }
        }

        private void BindCtrl(object request, Control ctrl)
        {
            if (!ctrl.HasChildren)
            {
                string fieldName = GetCtrlBindFieldName(ctrl);
                if (string.IsNullOrEmpty(fieldName))
                    return;

                string value = GetCtrlValue(ctrl);
                FieldInfo[] fields = request.GetType().GetFields();
                foreach (FieldInfo field in fields)
                {
                    if (field.Name.ToUpper() == fieldName.ToUpper())
                    {
                        field.SetValue(request, value);
                        break;
                    }
                }

                return;
            }

            foreach (Control c in ctrl.Controls)
            {
                BindCtrl(request, c);
            }
        }

        protected void Init()
        {
            InitCtrl(this);
        }

        private void InitCtrl(Control ctrl)
        {
            if (!ctrl.HasChildren)
            {
                string dictName = GetCtrlBindDictName(ctrl);

                InitCtrl(ctrl, dictName);

                return;
            }

            foreach (Control c in ctrl.Controls)
            {
                InitCtrl(c);
            }
        }

        protected void Reset()
        {
            ResetCtrl(this);
        }

        private void ResetCtrl(Control ctrl)
        {
            if (!ctrl.HasChildren)
            {
                string dictName = GetCtrlBindDictName(ctrl);

                ResetCtrl(ctrl, dictName);

                return;
            }

            foreach (Control c in ctrl.Controls)
            {
                ResetCtrl(c);
            }
        }

        private string GetCtrlBindDictName(Control ctrl)
        {
            string tag = Convert.ToString(ctrl.Tag);
            if (tag == null)
                tag = string.Empty;

            string[] tags = tag.Split(new string[] { TAG_SEPERATOR }, StringSplitOptions.None);
            string dictName = null;
            if (tags.Length == 1)
                dictName = tags[0].Trim();
            else if (tags.Length == 2)
                dictName = tags[1].Trim();

            return dictName;
        }

        private string GetCtrlBindFieldName(Control ctrl)
        {
            string tag = Convert.ToString(ctrl.Tag);
            if (tag == null)
                tag = string.Empty;

            string[] tags = tag.Split(new string[] { TAG_SEPERATOR }, StringSplitOptions.None);
            string fieldName = null;
            if (tags.Length >= 1)
                fieldName = tags[0].Trim();

            return fieldName;
        }

        protected string GetTradePassword()
        {
            /*string password = string.Empty;
            WinPassword frmPassword = new WinPassword();
            DialogResult result = frmPassword.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                frmPassword.Dispose();
            }

            if (result == DialogResult.OK)
            {
                password = frmPassword.Password;
                frmPassword.Dispose();
            }

            return password;*/

            return "123456";
        }

        protected override bool ProcessCmdKey(ref   Message msg, Keys keyData)
        {
            if (!(ActiveControl is Button) && !(ActiveControl is DevExpress.XtraEditors.BaseButton))
            {
                if (keyData == Keys.Enter || keyData == Keys.Down)
                {
                    SendKeys.Send("{TAB}");
                    return true;
                }

                if (keyData == Keys.Up)
                {
                    SendKeys.Send("+{Tab}");
                    return true;
                }
            }

            return base.ProcessCmdKey(ref   msg, keyData);
        }

    }
}
